package cn.acyou.leo.example.controller;

import cn.acyou.leo.example.service.StudentService;
import cn.acyou.leo.example.vo.Student;
import cn.acyou.leo.example.vo.StudentReq;
import cn.acyou.leo.framework.commons.PageQuery;
import cn.acyou.leo.framework.model.PageData;
import cn.acyou.leo.framework.model.PageSo;
import cn.acyou.leo.framework.model.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;

/**
 * @author fangyou
 * @version [1.0.0, 2021/7/29]
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/student")
@Api(value = "学生", description = "学生的增删改查", tags = "学生接口")
public class StudentController {

    @Autowired
    private StudentService studentService;

    @RequestMapping(value = "page", method = {RequestMethod.POST})
    @ApiOperation("测试分页")
    public Result<PageData<?>> studentsPage(@RequestBody PageSo pageSo) {
        PageData<?> convertType =  PageQuery.empty();
        return Result.success(convertType);
    }

    @RequestMapping(value = "get", method = {RequestMethod.GET})
    @ApiOperation("get")
    public Result<?> get(@RequestParam Long id, @RequestParam(required = false) String name) {
        Student stu = studentService.getById(id);
        return Result.success(stu);
    }

    @RequestMapping(value = "valid/test1", method = {RequestMethod.POST})
    @ApiOperation("测试参数校验1")
    public Result<?> validTest1(@Validated @RequestBody StudentReq studentReq){
        System.out.println("NAME" + studentReq);
        return Result.success(studentReq);
    }

    @GetMapping(value = "valid/test2")
    @ApiOperation("测试参数校验2")
    public Result<?> validTest2(@Validated @NotBlank(message = "name不能为空") String name){
        return Result.success();
    }
}
